import { reactive, ref } from 'vue'

export function useForm() {
  const initialValues = {
    code: null as string | null,
    name: null as string | null,
    shopManager: null as string | null,
    telephone: null as string | null,
    address: null as string | null,
    description: null as string | null
  }

  const state = reactive({
    detailFormRef: ref(),
    detailForm: { ...initialValues }
  })

  const meta = {
    model: state.detailForm,
    rules: {
      name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
      shopManager: [{ required: true, message: '负责人不能为空', trigger: 'blur' }],
      telephone: [
        {
          pattern: /^$|^1(3\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\d|9[0-35-9])\d{8}$/,
          message: '不是正确的手机号格式',
          trigger: ['blur', 'change']
        }
      ]
    }
  }

  const initForm = () => {
    state.detailForm = { ...initialValues }
  }

  const resetForm = () => {
    state.detailForm = { ...initialValues }
  }

  const getFormValues = () => state.detailForm

  const setDetail = (record: any) => {
    state.detailForm = { ...record }
  }

  return {
    meta,
    state,
    setDetail,
    initForm,
    resetForm,
    getFormValues
  }
}
